<!-- 设计用户的登入界面 -->
<template>
    <div class="login">
        <h1>登录</h1>
        <div class="login-wrapper">
            <div class="avatar">
                <img src="../assets/1.gif" alt="">
            </div>

            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]" />
                    <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                        登入
                    </van-button>
                </div>
            </van-form>

        </div>
        <!-- 点击跳转注册页面 -->
        <p class="register" @click="() => router.push('./register')">新用户？点击这里注册</p>
    </div>

</template>

<script setup>
import { ref } from 'vue'
import axios from '@/api/index'// 引入自己封装的axios
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const onSubmit = async (values) => {
    // 向后端地址发送请求，为后端接口地址传递值：这段代码就是接口请求
    const res = await axios.post('/user/login', {
        // http://localhost:3000/user/login，http://localhost:3000就是baseURL
        // 将值给后端，后端校验前端输入的值，根据值给出数据
        username: values.username,
        password: values.password
    })
    // 浏览器本地存储
    localStorage.setItem('userInfo', JSON.stringify(res.data))
    // 将后端传递过来的token存储到浏览器本地
    localStorage.setItem('token', res.token)

    router.push('/noteClass')
}
</script>

<style lang="less" scoped>
.login {
    width: 100vw;
    height: 100vh;
    background-color: #fff;
    padding: 0 0.3rem;
    box-sizing: border-box;
    overflow: hidden;
    position: relative;

    h1 {
        height: 0.6933rem;
        text-align: center;
        font-size: 0.48rem;
        margin-top: 1.12rem;
    }

    .login-wrapper {
        width: 7.44rem;
        height: 10.77rem;
        border: 1px solid rgba(187, 187, 187, 1);
        margin: 0 auto;
        margin-top: 1.7rem;
        border-radius: 0.3rem;
        box-shadow: 0 0 0.533rem rgba(170, 170, 170, 1);

        .avatar {
            width: 2.4rem;
            height: 2.4rem;
            margin: 1rem auto 0.77rem auto;
            // margin-bottom: 1rem;
            // border-radius: 50%;
            overflow: hidden;
            // animation: move 5s linear infinite;
            animation: move 3s cubic-bezier(0, 0, 1, 1) infinite;

            img {
                width: 100%;
            }
        }
    }

    .register {
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translate(-50%);
        font-size: 0.374rem;
        color: ragba(16, 16, 16, 1);
    }
}

:deep(.van-cell__title.van-field__label) {
    width: 45px;
}
</style>